package ding.blog.service;


import com.example.myblog.entity.Article;
import com.example.myblog.dao.ArticleRepository;
import com.example.myblog.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleService {

    private final ArticleRepository articleRepository;

    @Autowired
    public ArticleService(ArticleRepository articleRepository) {
        this.articleRepository = articleRepository;
    }

    // 保存文章
    public Article saveArticle(Article article) {
        return articleRepository.save(article);
    }

    // 根据ID获取文章
    public Article getArticleById(Long id) {
        return articleRepository.findById(id).orElse(null);
    }

    // 根据标题获取文章
    public List<Article> getArticlesByTitle(String title) {
        return articleRepository.findByTitleContaining(title);
    }

    // 根据分类获取文章
    public List<Article> getArticlesByCategory(String category) {
        return articleRepository.findByCategory(category);
    }

    // 通过文章获取评论
    List<Comment> findByArticle(Article article){
        return articleRepository.findByArticle(article);
    }
}